package com.example.demo.controller;


import com.example.demo.entity.Commodity;
import com.example.demo.entity.Supplier;
import com.example.demo.service.SupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("Supplier manager")
@RestController
@RequestMapping("/supplier")
public class SupplierController {
    @Autowired
    SupplierService service;

    @ApiOperation("Get supplier by id")
    @GetMapping("/{id}")
    public ResponseEntity<Supplier> getSupplier(@ApiParam("Supplier ID") @PathVariable long id){
        Supplier result = service.getSupplier(id);
        if(result == null)
            return ResponseEntity.noContent().build();
        else
            return ResponseEntity.ok(result);
    }

    @ApiOperation("Find supplier")
    @GetMapping("")
    public ResponseEntity<List<Supplier>> findCommodity(@ApiParam("Supplier name")String name){
        List<Supplier> result = service.findSupplier(name);
        return ResponseEntity.ok(result);
    }

    @ApiOperation("Add supplier infos")
    @PostMapping("")
    public ResponseEntity<Supplier> addSupplier(@RequestBody Supplier supplier){
        Supplier result = service.addSupplier(supplier);
        return ResponseEntity.ok(result);
    }

    @ApiOperation("Update Supplier info")
    @PutMapping("/{id}")
    public ResponseEntity<Void> updateSupplier(@PathVariable long id,@RequestBody Supplier supplier){
        service.updateSupplier(supplier);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteSupplier(@PathVariable long id){
        service.deleteSupplier(id);
        return ResponseEntity.ok().build();
    }

}
